package com.op.crm.webapp.action.security;

import com.op.crm.persist.model.User;
import com.op.crm.util.Constant;
import com.op.crm.util.Encrypt;
import com.op.crm.util.Utils;

public class LoginFunction extends SecurityFactory<User>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public User getModel() {
		if (userParam == null)
			userParam = new User();
		return userParam;
	}
	
	/**
	 * @return
	 * @throws Exception
	 * @author zhoujj
	 * CRM用户登录
	 */
	@Override
	public String execute() throws Exception {
		if (Utils.isBlank(userParam.getUserId())) {
			setReturnValue("错误:登录名为空!");
			return ERROR;
		}
		
		if (Utils.isBlank(userParam.getPassword())) {
			setReturnValue("错误:密码为空!");
			return ERROR;
		}
		
		String pwd = Encrypt.toMD5(userParam.getPassword());
		User loginUser = securityService.getUser(userParam.getUserId());
		
		if (loginUser == null) {
			setReturnValue("错误：用户不存在!");
			return ERROR;
		}
		
		if (!loginUser.getPassword().equals(pwd)) {
			setReturnValue("错误:密码错误!");
			return ERROR;
		}

		getSession().put(Constant.SESSION_CRM_USER, loginUser);
		userParam = null;
		
		
		return SUCCESS;
	}
	
	public static void main(String[] args) {
		System.out.println(Encrypt.toMD5("123"));
	}
}
